<?php
// error_reporting(1);
//ini_set('display_errors','On');
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Configuration;
use Doctrine\Common\Cache\ArrayCache;
use Doctrine\DBAL\DriverManager;

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {

    protected function __initSession() {
        Zend_Session::start();
    }

    protected function _initLog() {
        $writer = new Zend_Log_Writer_Stream(APPLICATION_PATH . '/logs/aplicacion.log');
        $logger = new Zend_Log($writer);
        Zend_Registry::set('logger', $logger);
    }
    
    /**
     * Register namespace Default_
     * @return Zend_Application_Module_Autoloader
     */
    protected function _initAutoload()
    {
        $autoloader = new Zend_Application_Module_Autoloader(array(
            'namespace' => 'Default_',
            'basePath'  => dirname(__FILE__),
        ));
        return $autoloader;
    }
    
    function _initAutoload2() {
        $loader = Zend_Loader_Autoloader::getInstance();
        $loader->registerNamespace('Doctrine');
        $moduleLoader = new Zend_Application_Module_Autoloader(array('namespace' => 'Doctrine', 'basePath' => APPLICATION_PATH.'/modules/Doctrine'));
        return $moduleLoader;

    }
    
    protected function _initDoctrine() {

        $config = new Configuration();
        $cache = new ArrayCache();

        $config->setMetadataCacheImpl($cache);
        $config->setQueryCacheImpl($cache);
        $config->setProxyDir(APPLICATION_PATH . "/../library" . DIRECTORY_SEPARATOR . 'Proxies');
        $config->setProxyNamespace('Proxies');

        $driverImpl = $config->newDefaultAnnotationDriver(array(APPLICATION_PATH . "/models/entity"));
        //$driverImpl->addPath('/path/to/lib/AnotherNs/Entities');
        $config->setMetadataDriverImpl($driverImpl);

        if ($this->getEnvironment() == "development") {
            $config->setAutoGenerateProxyClasses(true);
        } else {
            $config->setAutoGenerateProxyClasses(true);
        }

        $connPDO = array();
        $this->bootstrap('db');
        $connPDO['pdo'] = $this->getResource('db')->getConnection();
        $conn = DriverManager::getConnection($connPDO, $config);
        $em = EntityManager::create($conn, $config);
                
        Zend_Registry::set('em', $em);

        return $em;
    }

    /*
      protected function _initDefaultEmailTransport() {
      $emailConfig = $this->getOption('email');

      $smtpHost = $emailConfig['transportOptionsSmtp']['host'];
      unset($smtpHost);

      $mailTransport = new Zend_Mail_Transport_Smtp('smtp.gmail.com', $emailConfig['transportOptionsSmtp']);

      Zend_Mail::setDefaultTransport($mailTransport);
      }

     */

    function _initViewHelpers() {
//      $this->bootstrap('layout');
//      $layout = $this->getResource('layout');
//      $view = $layout->getView();
//
//      $view->setHelperPath(APPLICATION_PATH.'/helpers', '');
//      Zend_Dojo::enableView($view);
//
//      $view->doctype('HTML4_STRICT');
//      $view->headMeta()->appendHttpEquiv('Content-type', 'text/html;charset=utf-8')
//      ->appendName('description', 'Using view helpers in Zend_view');
//
//      // Return it, so that it can be stored by the bootstrap
//      return $view;

        $view = new Zend_View(array('encoding' => 'ISO-8859'));
//$view->addHelperPath('usuarios/view/helpers/', 'Usuarios_View_Helper_Mensajes');
        $view->addHelperPath("ZendX/JQuery/View/Helper", "ZendX_JQuery_View_Helper");
        $viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer();
        $viewRenderer->setView($view);
        Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);
       //  Zend_Controller_Action_HelperBroker::addPath(APPLICATION_PATH . '/helpers');
        return $view;
    }

//    protected function _initPlugins() {
//
//       Zend_Controller_Action_HelperBroker::addPath(APPLICATION_PATH . '/helpers');
//    }

}

